<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>行为维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../css/jeui.css"  media="all">
    <link rel="stylesheet" href="../css/skin/jebox.css"  media="all">
    <link rel="stylesheet" href="../css/skin/jedate.css"  media="all">
    <script type="text/javascript" src="../js/modules/jeui.js"></script>
    <script src="https://cdn.wilddog.com/sdk/js/2.5.17/wilddog.js"></script>
    <script src="../js/dataserver.js"></script>
    <style>
        
    </style>
</head>
<body>
<div class="je-p20">
    
    <blockquote class="je-quote green je-f16 je-ovh  je-mb10">
        <p class="je-pb5">
            <input type="text" name="title" autocomplete="off" placeholder="请输入关键字" class="je-input je-pl5 je-pr5">
            <button class="je-btn je-f14">查询</button>
            <button class="je-btn" id="addart"><i class="je-icon je-f20">&#xe66e;</i> 添加数据</button>
        </p>
    </blockquote>
    <table class="je-table je-mb20" id="newCheck">
        <thead>
        <tr>
            <th width="180" align="left">ID</th>
            <th>名称</th>
            <th>评分</th>
            <th>规划</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tablebody">
            <tr>
                <td colspan="5"  align="center">暂无数据</td>
            </tr>
        </tbody>
    </table>
    


    
</div>
<script type="text/javascript">

    function initTable() {
        data_getAllFrom("mydata/behaviors",function (data) {
            console.log(data);
            var html = [];
            for( key in data){
                if(key != "details"){
                    var obj = data[key];
                    html.push('<tr>');
                    html.push('<td>'+key+'</td>');
                    html.push('<td align="center">'+obj.mc+'</td>');
                    html.push('<td align="center">'+obj.fs+'</td>');
                    html.push('<td align="center">'+obj.gh+'</td>');
                    html.push('<td align="center"><button class="je-btn edit-btn je-btn-small je-f12">编辑</button> <button class="je-btn dele-btn je-btn-small je-bg-red je-f12">删除</button></td>');
                    html.push('</tr>');
                }

            }
            $("#tablebody").html(html.join(""));
        });
    }

    


    function autoData(dom) {
        var o = {};
        dom.find("[datakey]").each(function () {
            o[$(this).attr("datakey")] = $(this).val();
        });
        return o;
    }

    function getFrom(doms) {
        var val_mc = "";
        var val_fs = "";
        var val_dj = "";

        if(doms){
            val_mc = doms.eq(1).text();
            val_fs = doms.eq(2).text();
            val_dj = doms.eq(3).text();
        }

        var html = [];
        html.push('<div class="je-form-pane xwfrom">');
        html.push('<div class="je-form-item">');
        html.push('<label class="je-label je-f14">名称</label>');
        html.push('<div class="je-inputbox"><input type="text" name="title" autocomplete="off" class="je-input" datakey="mc" value="'+val_mc+'"></div>');
        html.push('</div>');
        html.push('<div class="je-form-item">');
        html.push('<label class="je-label je-f14">评分</label>');
        html.push('<div class="je-inputbox"><input type="text" name="title" autocomplete="off" class="je-input" datakey="fs" value="'+val_fs+'"></div>');
        html.push('</div>');
        html.push('<div class="je-form-item">');
        html.push('<label class="je-label je-f14">规划</label>');
        html.push('<div class="je-inputbox"><input type="text" name="title" autocomplete="off" class="je-input" datakey="gh" value="'+val_dj+'"></div>');
        html.push('</div>');
        html.push('</div>');
        return html.join("");
    }


    jeui.use(["jquery","jeBox","jeDate"],function () {
        initTable();
        $("#addart").click(function () {
            jeBox.open({
                title: "添加数据",
                boxSize:["400px","240px"],
                content:getFrom(),
                maskLock : true,
                button: [ {
                    name: '保存',
                    callback: function(index, id){
                        data_save("mydata/behaviors",[autoData($(".xwfrom"))]);
                        jeBox.close(index);
                        initTable();
                    }
                }]
            });
        });

        $("#tablebody").on('click', '.edit-btn', function(event) {
            var _tds = $(this).parent().siblings();
            event.preventDefault();
            jeBox.open({
                title: "修改数据",
                boxSize:["400px","240px"],
                content:getFrom(_tds),
                maskLock : true,
                button: [ {
                    name: '保存',
                    callback: function(index, id){
                        data_updata({
                            url : "mydata/behaviors/"+_tds.eq(0).text(),
                            data : autoData($(".xwfrom")),
                            success : function () {
                                jeBox.close(index);
                                initTable();
                            },
                            fail : function () {
                                jeBox.alert("修改失败");
                            }
                        });
                    }
                }]
            });
        });

        $("#tablebody").on('click', '.dele-btn', function(event) {
            var _tds = $(this).parent().siblings();
            data_delete({
                url : "mydata/behaviors/"+_tds.eq(0).text(),
                success : function () {
                    jeBox.alert("删除成功");
                    initTable();
                },
                fail : function () {
                    jeBox.alert("删除失败");
                }
            });
            event.preventDefault();
        });

    });
</script>
</body>
</html>